import Dialog from "../../../dist/dialog/dialog";
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current_id: "",
    leave_class: true,
    choose_date: "选择日期",
    current_elem_id: '',
    project_id: "",
    timestamp: "",
    project_name: '选择项目',
    show_tips: false,
    currentDate: new Date().getTime(),
    minDate: "",
    maxDate: "",
    show: false,
    list: [],
    tips_list: [],
    divColor: "white",
    page: 1,
    total_page: 1,
    store_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.store_id = options.store_id || '4';
    this.init();
    this.get_project_data();
  },

  init() {
    let year = new Date().getFullYear();
    let minDate = new Date(year - 90, 1, 1).getTime();
    let maxDate = new Date(year + 90, 1, 1).getTime();
    console.log(minDate, 'minDate===');
    this.setData({
      show: false,
      minDate,
      maxDate
    });

  },
  goto(e) {
    let {
      order_id,
      type
    } = e.currentTarget.dataset.item;
    if (type == 1) {
      wx.navigateTo({
        url: '/packageA/member/orderdetail/orderdetail?order_id=' + order_id + '&orderType=shop',
      })
    }

  },
  get_project_data() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.store-screen.frontend.controllers.cost.index.get-project-list"),
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          this.setData({
            tips_list: data
          });
        } else {
          app.tips(msg);
        }
      }
    })
  },
  getData(page = 1) {
    let json = {
      page,
      search: {},
      store_id: this.data.store_id
    };
    if (this.data.project_id) {
      json.search['project_id'] = this.data.project_id;
    }
    if (this.data.timestamp) {
      json.search['date'] = this.data.timestamp;
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.store-screen.frontend.controllers.cost.index"),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          let list = page == 1 ? data.data : this.data.list.concat(data.data);
          this.data.page = data.current_page;
          this.data.total_page = data.last_page;
          this.data.list = list;
          this.setData({
            list: this.data.list
          });
        } else {
          app.tips(msg);
        }
      }
    })
  },
  handle_tip() {
    if (!this.data.show_tips) {
      this.setData({
        show_tips: true,
        leave_class: false
      });
    } else {
      this.close_animation();
    }

  },
  handle_popup() {
    this.setData({
      show: !this.data.show
    });
  },
  hideDropdownIfOutside(event) {
    if (!event.currentTarget.dataset.item) {
      this.close_animation();
    }
  },
  close_animation(){
    this.setData({
      leave_class: true
    });
    setTimeout(() => {
      this.setData({
        show_tips: false
      });
    }, 500);
  },
  handle_edit(e) {
    let {
      id
    } = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/mircoApp/store_screen/form/form?id=' + id + '&store_id=' + this.data.store_id,
    })
  },
  handle_delete(e) {
    let {
      index,
      item
    } = e.currentTarget.dataset;
    console.log(index, 'index===');
    Dialog.confirm({
        title: '提示',
        message: '删除后无法恢复，确认删除吗？',
      })
      .then(() => {
        app._postNetWork({
          url: app.getNetAddresss("plugin.store-screen.frontend.controllers.cost.index.delete"),
          data: {
            store_id: this.data.store_id,
            id: item.id
          },
          success: resdata => {
            let {
              result,
              msg
            } = resdata.data;
            app.tips(msg)
            if (result) {
              this.setData({
                current_id: item.id
              });
              setTimeout(() => {
                this.data.list.splice(index, 1);
                this.setData({
                  list: this.data.list
                });
              }, 1000);

            }
          }
        })


      })
      .catch(() => {});
  },
  startTouch(e) {
    let divColor = this.data.divColor === 'white' ? '#F5F5F5' : 'white';
    this.setData({
      current_elem_id: e.currentTarget.dataset.item.id,
      divColor
    });
  },
  endTouch() {
    this.setData({
      divColor: "white"
    })
  },
  choose(e) {
    let { name, id } = e.currentTarget.dataset.item;
    this.data.project_id = this.data.project_name == name ? '' : id;
    this.close_animation();
    this.setData({
      project_name: this.data.project_name == name ? '请选择项目' : name
    })

    this.getData();
  },
  onConfirm(e) {
    let date = new Date(e.detail);
    let year = date.getFullYear(),
      month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1,
      day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    let choose_date = `${year}-${month}-${day}`;
    this.data.timestamp = Math.floor(new Date(e.detail).getTime() / 1000);
    this.setData({
      show: false,
      choose_date,
    });
    this.getData();
  },
  cancel() {
    this.setData({
      show: false
    });
  },
  handle_add() {
    wx.navigateTo({
      url: '/mircoApp/store_screen/form/form?store_id=' + this.data.store_id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page >= this.data.total_page) return;
    let page = this.data.page + 1;
    this.getData(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})